package com.example.eos.service;

import com.example.eos.bean.Reply;
import com.example.eos.bean.ReturnInfo;
import com.example.eos.bean.User;
import com.example.eos.mapper.CommentMapper;
import com.example.eos.mapper.ReplyMapper;
import com.example.eos.mapper.UserMapper;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ReplyService {

    private ReplyMapper replyMapper;
    private CommentMapper commentMapper;
    private UserMapper userMapper;

    @Autowired
    public ReplyService(ReplyMapper replyMapper, CommentMapper commentMapper, UserMapper userMapper){
        this.replyMapper = replyMapper;
        this.commentMapper = commentMapper;
        this.userMapper = userMapper;
    }

    //查看回复
    public ReturnInfo getReply(String replyid){
        if(replyMapper.findByReplyid(replyid)==null){
            return ReturnInfo.info2("失败","");
        }else {
            return ReturnInfo.info2("成功",replyMapper.findByReplyid(replyid));
        }
    }

    //查看回复列表
    public ReturnInfo getReplys(String commentid){
        if(commentMapper.findByCommentid(commentid)==null){
            return ReturnInfo.info2("失败","");
        }else {
            List<Reply> list = replyMapper.findByComment(commentid);
            return ReturnInfo.info2("成功",list);
        }
    }

    //新建回复
    public ReturnInfo creatReply(Reply reply){
        User user =(User) SecurityUtils.getSubject().getPrincipal();
        reply.setUserid(user.getUserid());
        if(!reply.getTouserid().equals("")&&userMapper.findByUserid(reply.getTouserid())==null){
            return ReturnInfo.info1("失败");
        }else {
            replyMapper.save(reply.completReply(reply));
            return ReturnInfo.info1("成功");
        }
    }

    //删除回复
    public ReturnInfo deleteReply(String replyid){
        if(replyMapper.findByReplyid(replyid)==null){
            return ReturnInfo.info1("失败");
        }else {
            replyMapper.deleteByReplyid(replyid);
            return ReturnInfo.info1("成功");
        }
    }
}
